<?php
class StudentController
{
	static $instance;

	private function __construct() {

	}

	protected function __clone() {

	}

	static public function getInstance() {
		if(is_null(self::$instance))
		{
			self::$instance = new self();
		}
		return self::$instance;
	}

	function getActivityOnDay($studentId, $activity_type, $strDays)
	{


		$strSQL = "SELECT
						activity.*
				 	FROM 
				 		student_vs_activity 
					  	INNER JOIN activity  		ON activity.ID 		= student_vs_activity.ACTIVITY_ID
					  	INNER JOIN activity_type  	ON activity_type.ID = activity.ACTIVITY_TYPE_ID
					  	INNER JOIN student  		ON student.ID 		= student_vs_activity.STUDENT_ID  
				  	WHERE 
				  		student_vs_activity.STUDENT_ID = ? AND activity_type.ID = ? AND activity.DAYS = ?;";

		$objRs =	GlobalApp::getInstance()->executeSQL($strSQL, array($studentId, $activity_type, $strDays));

		//print $objRs->sql;

		return $objRs;
	}

	function hasActivityOnDay($studentId, $activity_type, $strDays)
	{
		$isFound = false;

		$strSQL = "SELECT
						COUNT(student_vs_activity.STUDENT_ID) AS TOTAL
				 	FROM 
				 		student_vs_activity 
					  	INNER JOIN activity  		ON activity.ID 		= student_vs_activity.ACTIVITY_ID
					  	INNER JOIN activity_type  	ON activity_type.ID = activity.ACTIVITY_TYPE_ID
					  	INNER JOIN student  		ON student.ID 		= student_vs_activity.STUDENT_ID  
				  	WHERE 
				  		student_vs_activity.STUDENT_ID = ? AND activity_type.ID = ? AND activity.DAYS = ?;";

		$objRs =	GlobalApp::getInstance()->executeSQL($strSQL, array($studentId, $activity_type, $strDays));

		
		if ( $objRs->fields["TOTAL"] > 0  )
		{
			$isFound = true;
		}

		return $isFound;
	}

	function removeStudentActivity($studentId, $activityId)
	{
			$arrData = array($studentId, $activityId);

			$strSQL = "DELETE FROM student_vs_activity WHERE STUDENT_ID = ? AND ACTIVITY_ID = ?;";	
				
			GlobalApp::getInstance()->executeSQL($strSQL, $arrData);
	}	

	function confirmStudentActivity($studentId, $activityId)
	{
			$arrData = array($studentId, $activityId);

			$strSQL = "UPDATE student_vs_activity SET IS_BOUGHT = 'S' WHERE STUDENT_ID = ? AND ACTIVITY_ID = ?;";	
				
			GlobalApp::getInstance()->executeSQL($strSQL, $arrData);
	}	
	
	function saveStudentActivity($studentId, $activityId)
	{
		$isRegistered = false;


		if (ActivityController::getInstance()->hasPlaces($activityId) )
		{
			$arrData = array($studentId, $activityId);


			$strSQL = "INSERT INTO student_vs_activity
						(
							STUDENT_ID, 
							ACTIVITY_ID,
							IS_BOUGHT

						) 
						VALUES
						(
							?, 
							?,
							'N'
						);";	
				
			GlobalApp::getInstance()->executeSQL($strSQL, $arrData);
				
			$isRegistered = true;
		}



		return $isRegistered;
	}

	function saveStudent($Name, $Email, $MobilePhoneNumber,   $isIndication)
	{
		$arrDataStudent = array($Name, $Email, $MobilePhoneNumber,  $isIndication);



		$strSQL = "INSERT INTO student
		(
			NAME, 
			EMAIL, 
			MOBILE_PHONE_NUMBER, 
			LAST_UPDATE, 
			IS_INDICATION
		) 
		VALUES
		(
			?, 
			?, 
			?, 
			CURDATE(), 
			?
		);";


		GlobalApp::getInstance()->executeSQL($strSQL, $arrDataStudent);
	}

	function getStudent($studentId)
	{
		$objRs =	GlobalApp::getInstance()->executeSQL("SELECT * FROM student WHERE ID = ?", array($studentId));
		return $objRs;
	}

	function hasTypeActivity($studentId, $activityType)
	{
		$isFound = false;

		$strSQL = "SELECT
						COUNT(student_vs_activity.STUDENT_ID) AS TOTAL
				 	FROM 
				 		student_vs_activity 
					  	INNER JOIN activity  		ON activity.ID 		= student_vs_activity.ACTIVITY_ID
					  	INNER JOIN activity_type  	ON activity_type.ID = activity.ACTIVITY_TYPE_ID
					  	INNER JOIN student  		ON student.ID 		= student_vs_activity.STUDENT_ID  
				  	WHERE 
				  		student_vs_activity.STUDENT_ID = ? AND activity_type.ID = ?";

		$objRs =	GlobalApp::getInstance()->executeSQL($strSQL, array($studentId, $activityType));

	
		if ( $objRs->fields["TOTAL"] > 0  )
		{

			$isFound = true;
		}

		return $isFound;
	}

	function hasAnyActivity($studentId)
	{
		$isFound = false;


		$objRs =	GlobalApp::getInstance()->executeSQL("SELECT * FROM student_vs_activity WHERE STUDENT_ID = ? AND ACTIVITY_ID = ?", array($studentId));

		if ( is_array($objRs->fields)  )
		{

			$isFound = true;
		}

		return $isFound;
	}	
	
	function hasActivity($studentId, $ActivityId)
	{
		$isFound = false;


		$objRs =	GlobalApp::getInstance()->executeSQL("SELECT * FROM student_vs_activity WHERE STUDENT_ID = ? AND ACTIVITY_ID = ?", array($studentId, $ActivityId));

		if ( is_array($objRs->fields)  )
		{

			$isFound = true;
		}

		return $isFound;
	}

	function getStudentByEmail($studentEmail)
	{
		$objRs =	GlobalApp::getInstance()->executeSQL("SELECT * FROM student WHERE email = ?", array($studentEmail));
		return $objRs;
	}

}
?>